package com.hnu.model;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Bookmarkbackup entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.hnu.model.Bookmarkbackup
 * @author MyEclipse Persistence Tools
 */

public class BookmarkbackupDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(BookmarkbackupDAO.class);
	// property constants
	public static final String BOOKMARK_TITLE = "bookmarkTitle";
	public static final String BOOKMARK_DESC = "bookmarkDesc";
	public static final String BOOKMARK_URL = "bookmarkUrl";

	protected void initDao() {
		// do nothing
	}

	public int getBookmarkbackupTotalCount() {
		String hql = "SELECT count(*) FROM Bookmarkbackup";
		int allCount = ((Long) (this.getHibernateTemplate().find(hql)
				.iterator().next())).intValue();
		return allCount;
	}

	@SuppressWarnings("unchecked")
	public List<Bookmarkbackup> getOneUnitBookmarkbackup(final int unitSize) {
		Object dataList = this.getHibernateTemplate().executeFind(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "FROM Bookmarkbackup as bookmarkbackup";
						Query q = session.createQuery(hql);
						q.setFirstResult(0);
						q.setMaxResults(unitSize);
						return q.list();
					}
				});
		return (List<Bookmarkbackup>) dataList;
	}
	
	public boolean deleteBookmarkbackup(List<Bookmarkbackup> backupList)
	{
		this.getHibernateTemplate().deleteAll(backupList);
		return true;
	}

	public void save(Bookmarkbackup transientInstance) {
		log.debug("saving Bookmarkbackup instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Bookmarkbackup persistentInstance) {
		log.debug("deleting Bookmarkbackup instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Bookmarkbackup findById(java.lang.Long id) {
		log.debug("getting Bookmarkbackup instance with id: " + id);
		try {
			Bookmarkbackup instance = (Bookmarkbackup) getHibernateTemplate()
					.get("com.hnu.model.Bookmarkbackup", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Bookmarkbackup instance) {
		log.debug("finding Bookmarkbackup instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Bookmarkbackup instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Bookmarkbackup as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Bookmarkbackup instances");
		try {
			String queryString = "from Bookmarkbackup";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Bookmarkbackup merge(Bookmarkbackup detachedInstance) {
		log.debug("merging Bookmarkbackup instance");
		try {
			Bookmarkbackup result = (Bookmarkbackup) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public static BookmarkbackupDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (BookmarkbackupDAO) ctx.getBean("BookmarkbackupDAO");
	}
}